﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WMS.Interfactructure.Interfaces;

namespace WMS.Interfactructure.Implements
{
    /// <summary>
    /// 仓储基类
    /// </summary>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        /// <summary>
        /// 上下文
        /// </summary>
        DataDbContext db;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db">上下文</param>
        public BaseRepository(DataDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 新增实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddAsync(T entity)
        {

            try
            {
                await db.Set<T>().AddAsync(entity);

                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 批量新增实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddRangeAsync(List<T> entity)
        {

            try
            {
                await db.Set<T>().AddRangeAsync(entity);

                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns>返回数据集</returns>
        public IQueryable<T> GetAll()
        {
            try
            {
                return db.Set<T>();
            }
            catch (Exception)
            {

                throw;
            }


        }
        /// <summary>
        /// 有条件的查询数据
        /// </summary>
        /// <param name="expression">查询参数</param>
        /// <returns>返回数据集</returns>
        public IQueryable<T> GetAll(Expression<Func<T, bool>> expression)
        {

            try
            {
                return db.Set<T>().Where(expression);
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 通过主键查询一条数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回一条数据</returns>
        public async Task<T> FindAsync(int id)
        {

            try
            {
                return await db.Set<T>().FindAsync(id);
            }
            catch (Exception)
            {

                throw;
            }


        }
        /// <summary>
        /// 有条件的返回一条数据
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <returns>返回受影响行数</returns>
        public async Task<T> FindAsync(Expression<Func<T, bool>> expression)
        {

            try
            {
                return await db.Set<T>().FirstOrDefaultAsync(expression);
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 修改用户实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T entity)
        {
            try
            {
                db.Set<T>().Update(entity);

                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改用户实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateRangeAsync(List<T> entity)
        {
            try
            {
                db.Set<T>().UpdateRange(entity);

                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int id)
        {
            try
            {
                var list = await db.Set<T>().FindAsync(id);
                db.Remove(list);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                var list = await db.Set<T>().FirstOrDefaultAsync(expression);

                db.Remove(list);

                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> DeletteRangeAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                var list = db.Set<T>().Where(expression);

                db.RemoveRange(list);

                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
